<!--
  功能：学生管理
  作者：licy
  邮箱：licy@briup.com
  时间：2021年01月13日 13:31:28
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='student_list'>
    <!-- 按钮 -->
    <div class="btns">
      <div style="text-align:center;margin-bottom:.5em">
        <el-form size="small" :inline='true'>
          <el-form-item>
            <el-cascader filterable v-model="param.channelIds" clearable :options="channels" placeholder="请选择渠道" :props="{ checkStrictly: true ,children:'children',label:'name',value:'id', emitPath: false,multiple:true}" ></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader filterable v-model="param.clazzId" clearable :options="directionClazzTree" placeholder="请选择班级"
          :props="{ children:'clazzes',label:'name',value:'id',emitPath: false}" ></el-cascader>
          </el-form-item>
          <el-form-item >
            <el-select v-model="param.status" placeholder="状态" style="width:120px" clearable>
              <el-option value="报名中" label="报名中"></el-option>
              <el-option value="待选方向" label="待选方向"></el-option>
              <el-option value="待分班" label="待分班"></el-option>
              <el-option value="学习中" label="学习中"></el-option>
            </el-select>  
          </el-form-item>
          <el-form-item >
            <el-input placeholder="姓名" v-model="param.realname" ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" icon="el-icon-search" @click="loadStudents">搜索</el-button>
            <el-button size="small" type="primary" icon="el-icon-download" @click="toDownloadStudentsHandler">导出</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- 按钮 -->
    <!-- 表格 -->
    <el-table ref="multipleTable" :data="studentData.list" size="small" v-loading="loading">
      <el-table-column type="index" :index="1" label="序号" align="center" fixed="left"/>
      <el-table-column prop="realname" label="姓名" width="90" align="center" fixed="left"/>
      <el-table-column prop="clazz" label="杰普班级" min-width="200" align="left" fixed="left"/>
      <el-table-column prop="status" label="状态" width="80" align="center" fixed="left">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status ==='报名中'" type="info" size="mini">{{scope.row.status}}</el-tag>
          <el-tag v-else-if="scope.row.status==='待分班'" type="warning" size="mini">{{scope.row.status}}</el-tag>
          <el-tag v-else-if="scope.row.status==='离校'" type="danger" size="mini">{{scope.row.status}}</el-tag>
          <el-tag v-else type="success" size="mini">{{scope.row.status}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="学校" width="150" align="left">
        <template slot-scope="scope">
          <div v-if="scope.row.channel">
            {{scope.row.channel.name}}
          </div>
          <div v-else>
            暂无信息
          </div>
        </template>
      </el-table-column>
        <el-table-column label="学院" width="150" align="left">
        <template slot-scope="scope">
          <div v-if="scope.row.channel">
              {{scope.row.channel.parent?scope.row.channel.parent.name:""}}
          </div>
          <div v-else>
            暂无信息
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="major" label="专业" width="200" align="left"/>
      <el-table-column prop="schoolStuClazz" label="校内班级" width="200" align="left"/>
      <el-table-column prop="schoolStuNo" label="校内学号" width="150" align="center"/>
      <el-table-column prop="gender" label="性别" width="60" align="center"/>
      <el-table-column prop="nation" label="民族" width="80" align="center"/>
      <el-table-column prop="birth" label="出生日期" width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.birth | fmtDate}}
        </template>
      </el-table-column>
      <el-table-column prop="education" label="学历" width="80" align="center"/>
      <el-table-column prop="graduationTime" label="毕业时间" width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.graduationTime | fmtDate}}
        </template>
      </el-table-column>
      <el-table-column prop="flp" label="外语水平" width="80" align="center"/>
      <el-table-column prop="isAttention" label="是否关注" width="120" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isAttention" type="primary" size="small">{{scope.row.isAttention}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="comment" label="备注" width="180" align="center"/>
    </el-table>
    <!-- /表格 -->
     <!-- 导出配置模态框 -->
    <el-dialog title="导出配置" :visible.sync="dialog_visible" width="30%" class="customer_modal">
      <div style="text-align:center;padding:20px 0">
        <el-button size="small" @click="downloadHandler('current')">导出当前页</el-button>
        <el-button size="small" type="primary" @click="downloadHandler('all')">全部导出</el-button>
      </div>
    </el-dialog>
    <!-- /模态框 -->
    <!-- 分页 -->
    <div class="page">
        <el-pagination background 
          layout="prev, pager, next,sizes" 
          small
          :total="studentData.total" 
          :page-size="studentData.pageSize" 
          :current-page="studentData.page"
          :page-sizes="[10, 15, 20,25,50,100]"
          @size-change="handleSizeChange"
          @current-change="currentChangeHandler">
        </el-pagination>
    </div>
    <!-- /分页 -->
  </div>
</template>

<script>
import {get,post,download} from '@/utils/request'
import _ from 'lodash'
import { mapGetters } from 'vuex'
import qs from 'qs'
import Tinymce from '@/components/Tinymce'
import moment from 'moment'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
     visible:false,
     dialog_visible:false,
     loading:false,
     param:{
       page:1,
       pageSize:15
     },
     studentData:{list:[]},
   }
  },
  components: { Tinymce },
  // 计算属性
  computed: {
    ...mapGetters(['channels','directionClazzTree','user']),
  },
  // 侦听器
  watch: {
   'param.dc':{
     handler(now){
       this.param.directionId = now[0];
       this.param.clazzId = now[1];
     }
   }
  },
  // 组件方法
  methods: {
     // 下载数据
    downloadHandler(type){
      // 去除空值
      let param = _.clone(this.param)
      for(let key in param){
        if(!param[key]){
          delete param[key]
        }
      }
      if(param.channelId){
        param.channelIds = [param.channelId]
      } else {
        delete param.channelIds;
      }
      switch(type){
        case "current":
          break;
        case "all":
          param.pageSize = 10000;
          break;
      }
      download('/student/primary/download',param).then(resp => {
        // 关闭模态框
        this.dialog_visible = false;
      })
    },
    // 下载学生档案
    toDownloadStudentsHandler(){
      this.dialog_visible = true;
    },
    //处理visible变化的钩子函数,解决抽屉无法收缩的bug
    handlerVisibleChange(val){
      this.visible = val;
    },
    handlerVisibleChangeClazzChange(val){
      this.visible_change_clazz = val;
    },
    // 加载学生信息
    loadStudents(){
      this.loading = true;
      let url = '/student/primary/pageQuery'
      // 去除空值
      for(let key in this.param){
        if(!this.param[key]){
          delete this.param[key]
        }
      }
      if(this.param.channelId){
        this.param.channelIds = [this.param.channelId]
      }
      get(url+"?"+qs.stringify(this.param,{ arrayFormat: 'repeat' }))
      .then(response => {
        this.studentData = response.data
        this.loading = false;
      })
    },
     // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadStudents();
    },
     // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadStudents();
    },
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.loadStudents();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped>

</style>
